package GUI;

import order.*;
import model.*;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;


import GUI.MapPanel.TerritoryPanel;

public class OrderFrame extends JFrame{
	 private JButton submitButton, cancelButton;
	 private Territory territory;
	 private Player player;
	 private TerritoryPanel territoryPanel;
	 private TextPanel textPanel;
	 private Map map;

	 public OrderFrame(TextPanel _textPanel, TerritoryPanel _territoryPanel, Map _map, String orderType){
		super("Create a new order");
		territoryPanel = _territoryPanel;
		territory = territoryPanel.getTerritory();
		player = territoryPanel.getPlayer();
		map = _map;
		textPanel = _textPanel;
		
  	  	
  	    submitButton = new JButton("Submit");
  	    cancelButton = new JButton("Cancel");
  	    
  	    switch (orderType){
  	        case "Attack": new AttackFrame();
  	             break;
  	        case "Move": new MoveFrame();
  	             break;
  	        case "Convert back spy": new ConvertBackSpyFrame();
  	             break;
  	        case "Create spy" : new CreateSpyFrame();
  	             break;
  	        case "Move spy": new MoveSpyFrame();
  	             break;
  	        case "Upgrade troop": new UpgradeTroopFrame();
  	             break;
  	        default:
  	    }
  	       
	 }
	    
	 class cancelListener implements ActionListener{
		JFrame frame;
		public cancelListener(JFrame _frame){frame = _frame;}
		public void actionPerformed(ActionEvent e) {frame.dispose();}
     }
	 
	 class AttackFrame extends JFrame{
		 private JComboBox<Integer>[] troopAmountBox;
		 private JComboBox<Integer> destinationBox; 
		 private Attack temp;
		 
		 public AttackFrame(){
			this.setLayout(new GridLayout(9, 2, 10, 10));	   
			this.add(new JLabel("Use troops from " + territory.getLocation()));
		  	this.add(new JLabel("    "));
		  	
		  	troopAmountBox = new JComboBox[6];
		  	int i = 0;
		  	for (TechnologyLevel level: TechnologyLevel.values()){
		  	   this.add(new JLabel(level.toString()));
		  	   troopAmountBox[i] = new JComboBox<>();
		  	   troopAmountBox[i].addItem(0);
		  	   for (int j = 1; j <= territory.getUsableTroopAmount(level, player); j++)
		  		 troopAmountBox[i].addItem(j);
		  	   this.add(troopAmountBox[i]);
		  	   i++;
		  	}
		  	this.add(new JLabel("Attack Target: "));
		  	destinationBox = new JComboBox<Integer>();
		  	for (Territory neighbor: territory.getNeighbors())
		  		if ((neighbor.getOwner() != player) && (!neighbor.getOwner().isAlly(player)))//&& neighbor.getOwner()!= default
		  		  destinationBox.addItem(neighbor.getLocation());
		  	this.add(destinationBox);
		  	this.add(submitButton);
		  	this.add(cancelButton);
		  	submitButton.addActionListener(new submitListener(this));
		  	cancelButton.addActionListener(new cancelListener(this));
		  	this.setSize(600, 600);
		  	this.setVisible(true);
		 }
		 
		 class submitListener implements ActionListener{
			JFrame frame;
		    public submitListener(JFrame _frame){frame = _frame;}
		    public void actionPerformed(ActionEvent arg0) {
		    	Territory destination = map.getTerritory((int)destinationBox.getSelectedItem());
			    int i = 0;
			    HashMap<TechnologyLevel, Integer> trooplist = new HashMap<TechnologyLevel, Integer>();
			    for (TechnologyLevel level:TechnologyLevel.values()){
			       trooplist.put(level, (int)troopAmountBox[i].getSelectedItem());
			       i++;
			    }  
			    temp = new Attack(territory, player, destination, trooplist);
		  	    if (temp.checkOrder(map)){
			  	    textPanel.addHistory(temp);
			  	    temp.executeOrder();
					textPanel.updatePlayerPanel();
					territoryPanel.update();
			    }
		  	    frame.dispose();
		     }
		 }
	}
	 class MoveFrame extends JFrame{
		 private JComboBox<Integer>[] troopAmountBox;
		 private JComboBox<Integer> destinationBox; 
	     private Move temp;

		 public MoveFrame(){
			this.setLayout(new GridLayout(9, 2, 10, 10));	   
		  	this.add(new JLabel("Move from " + territory.getLocation()));
		  	this.add(new JLabel("    "));
		  
		  	troopAmountBox = new JComboBox[6];
		  	
		  	int i = 0;
		  	for (TechnologyLevel level: TechnologyLevel.values()){
		  	   this.add(new JLabel(level.toString()));
		  	   troopAmountBox[i] = new JComboBox<>();
		  	   troopAmountBox[i].addItem(0);
		  	   for (int j = 1; j <= territory.getUsableTroopAmount(level, player); j++)
		  		   troopAmountBox[i].addItem(j);
		  	   this.add(troopAmountBox[i]);
		  	   i++;
		  	}
		  	
		  	this.add(new JLabel("To Destiantion: "));
		  	destinationBox = new JComboBox<Integer>();
		  	for (Territory t : player.getTerritoryList())
		  	   if (map.routeProcess(territory, t))
		  		destinationBox.addItem(t.getLocation());
		  	for (Player p: player.getAllyList())
		  		for (Territory t: p.getTerritoryList())
		  			destinationBox.addItem(t.getLocation());
		  	
		  	this.add(destinationBox);
		  	
		  	this.add(submitButton);
		  	this.add(cancelButton);
		  	submitButton.addActionListener(new submitListener(this));
		  	cancelButton.addActionListener(new cancelListener(this));
		  	
		  	setSize(600, 600);
		  	setVisible(true);
		 }
		 
		 class submitListener implements ActionListener{
	   
			JFrame frame;
		  public submitListener(JFrame _frame){
				frame = _frame;
			}
		  public void actionPerformed(ActionEvent arg0) {
			Territory destination = map.getTerritory((int)destinationBox.getSelectedItem());
			int i = 0;
			HashMap<TechnologyLevel, Integer> trooplist = new HashMap<TechnologyLevel, Integer>();
			for (TechnologyLevel level:TechnologyLevel.values()){
			   trooplist.put(level, (int)troopAmountBox[i].getSelectedItem());
			   i++;
			}  
		  	 temp = new Move(territory, player, destination, trooplist);
		  	 if (temp.checkOrder(map)){
			  	    textPanel.addHistory(temp);
			  	    temp.executeOrder();
			  	    textPanel.updatePlayerPanel();
			  	    territoryPanel.update();
			  	    territoryPanel.getMapPanel().getTerritoryPanel(destination).update();
			  }
		  	 frame.dispose();
		}
		 }
		 
	}
	 class ConvertBackSpyFrame extends JFrame{
		 
		 private ConvertBackSpyFrame convertBackSpyFrame;
		 private JComboBox<TechnologyLevel> sourceBox;
		 private ConvertBackSpy temp;

		 public ConvertBackSpyFrame(){
	     	convertBackSpyFrame = this;
			setLayout(new GridLayout(2, 2, 10, 10));	   
			
			this.add(new JLabel("Convert back a spy:  "));	
		  	sourceBox= new JComboBox<TechnologyLevel>();
		  	for (Spy spy : player.getSpyList())
		  		if (spy.getTerritory() == territory)
		  		  sourceBox.addItem(spy.getLevel());
		  	
		  	this.add(sourceBox);
		  	this.add(submitButton);
		  	this.add(cancelButton);
		  	submitButton.addActionListener(new submitListener());
		  	cancelButton.addActionListener(new cancelListener(this));
		  	
		  	this.setSize(400, 150);
		  	this.setVisible(true);
		 }
		 

		 class submitListener implements ActionListener{
	   
		  public void actionPerformed(ActionEvent arg0) {
			TechnologyLevel source = (TechnologyLevel)sourceBox.getSelectedItem();
			  for(Spy spy: player.getSpyList())
				  if ((spy.getTerritory() == territory) && (spy.getLevel() == source)){
					  temp = new ConvertBackSpy(spy); 
					  break;
				  }
		
		  	 if (temp.checkOrder()){
			  	    textPanel.addHistory(temp);
			  	    temp.executeOrder();
			  	    textPanel.updatePlayerPanel();
			  	    territoryPanel.update();
			     }
		  	 convertBackSpyFrame.dispose();
		}
		 }
	}
	 class CreateSpyFrame extends JFrame{
		 
		 private CreateSpyFrame createSpyFrame;
		 private JComboBox<TechnologyLevel> sourceBox;
		 private CreateSpy temp;

		 public CreateSpyFrame(){
			createSpyFrame = this;

			this.setLayout(new GridLayout(2, 2, 10, 10));	   
			
			this.add(new JLabel("Create a new Spy using:  "));	
		  	sourceBox= new JComboBox<TechnologyLevel>();
		  	for (TechnologyLevel level : TechnologyLevel.values())
		  		if (territory.getTroopAmount(level, player) > 0)
		  			sourceBox.addItem(level);
			  	
		  	this.add(sourceBox);
		  	this.add(submitButton);
		  	this.add(cancelButton);
		  	submitButton.addActionListener(new submitListener());
		  	cancelButton.addActionListener(new cancelListener(this));
		  	
		  	this.setSize(400, 150);
		  	this.setVisible(true);
		 }
		 

		 class submitListener implements ActionListener{
	   
		  public void actionPerformed(ActionEvent arg0) {
			TechnologyLevel source = (TechnologyLevel)sourceBox.getSelectedItem();
			  temp = new CreateSpy(territory, player, source);
		  	 if (temp.checkOrder()){
			  	    textPanel.addHistory(temp);
			  	    temp.executeOrder();
			  	   textPanel.updatePlayerPanel();
					territoryPanel.update();
			     }
		  	 createSpyFrame.dispose();
		}
		 }
		 
	}
	 class MoveSpyFrame extends JFrame{
		 private JComboBox<TechnologyLevel> spyBox;
		 private JComboBox<Integer> destinationBox; 
	     private MoveSpy temp;

		 public MoveSpyFrame(){		
			this.setLayout(new GridLayout(4, 2, 10, 10));	   
		  	this.add(new JLabel("Move spy: ")); 
		  	spyBox = new JComboBox<TechnologyLevel>();
		  	for (Spy spy: player.getSpyList()){
		  	   if ((spy.getTerritory() == territory) && (!spy.getActioned())) 
		  		   spyBox.addItem(spy.getLevel());
		  	}
		  	this.add(spyBox);
		  	this.add(new JLabel("at "));
		  	this.add(new JLabel(String.valueOf(territory.getLocation())));
		  	this.add(new JLabel("To Destiantion: "));
		  	destinationBox = new JComboBox<Integer>();
		  	for (Territory t : territory.getNeighbors()) //&& t.getOwner != default
		  	   destinationBox.addItem(t.getLocation());
		  	this.add(destinationBox);
		  	this.add(submitButton);
		  	this.add(cancelButton);
		  	submitButton.addActionListener(new submitListener(this));
		  	cancelButton.addActionListener(new cancelListener(this));
		  	
		  	this.setSize(400, 200);
		  	this.setVisible(true);
		 }
		 
		 class submitListener implements ActionListener{
	   
			JFrame frame;
		  public submitListener(JFrame _frame){
				frame = _frame;
			}
		  public void actionPerformed(ActionEvent arg0) {
			Territory destination = map.getTerritory((int)destinationBox.getSelectedItem());
			TechnologyLevel spylevel = (TechnologyLevel)spyBox.getSelectedItem();
			HashMap<TechnologyLevel, Integer> trooplist = new HashMap<TechnologyLevel, Integer>();
			trooplist.put(spylevel, 1);
			
		  	 temp = new MoveSpy(territory, player, destination, trooplist);
		  	 if (temp.checkOrder(map)){
			  	    textPanel.addHistory(temp);
			  	    temp.executeOrder();
			  	    textPanel.updatePlayerPanel();
			  	  territoryPanel.update();
			  	  territoryPanel.getMapPanel().getTerritoryPanel(destination).update();
			  }
		  	 frame.dispose();
		}
		 }
		 
	}     
	 class UpgradeTroopFrame extends JFrame{
		 
		 private UpgradeTroopFrame upgradeOrderFrame;
		 private JComboBox<Integer> troopAmountBox;
		 private JComboBox<TechnologyLevel> sourceBox;
		 private JComboBox<TechnologyLevel> destinationBox;
		 private UpgradeTroop temp;

		 public UpgradeTroopFrame(){
			upgradeOrderFrame = this;
			this.setLayout(new GridLayout(4, 2, 10, 10));	   
		  	this.add(new JLabel("Upgrade troops from: "));
		  	sourceBox = new JComboBox<TechnologyLevel>();
		  	sourceBox.addItem(null);
		  	for (TechnologyLevel level: TechnologyLevel.values())
		  	   if (territory.getTroopAmount(level, player) > 0)
		  		   sourceBox.addItem(level);
		  	sourceBox.addActionListener(new sourceSelectedListener());
		  	this.add(sourceBox);
		  	troopAmountBox = new JComboBox<Integer>();
		  	destinationBox = new JComboBox<TechnologyLevel>();
		  	this.add(new JLabel("To: "));
		  	this.add(destinationBox);
		  	this.add(new JLabel("Amount: "));
		  	this.add(troopAmountBox);
		  	
		  	this.add(submitButton);
		  	this.add(cancelButton);
		  	submitButton.addActionListener(new submitListener());
		  	cancelButton.addActionListener(new cancelListener(this));
		  	
		  	setSize(400, 200);
		  	setVisible(true);
		 }
		 
	     class sourceSelectedListener implements ActionListener{

			public void actionPerformed(ActionEvent e) {
				JComboBox cb = (JComboBox)e.getSource();
				TechnologyLevel Source = (TechnologyLevel)cb.getSelectedItem();
	      			troopAmountBox.removeAllItems();
	      			destinationBox.removeAllItems();
	      			troopAmountBox.addItem(0);
	      		  	for (int i = 1; i <= territory.getTroopAmount(Source, player); i++)
	      		  		troopAmountBox.addItem(i);
	      		  for (TechnologyLevel level : TechnologyLevel.values())
	      	  		if ((level.CombatBonus() > Source.CombatBonus()) && (level.CombatBonus() <= territory.getOwner().getLevel().CombatBonus()))
	      	  		  destinationBox.addItem(level);
			}
	    	 
	     }
		 class submitListener implements ActionListener{
	   
		  public void actionPerformed(ActionEvent arg0) {
			TechnologyLevel source = (TechnologyLevel)sourceBox.getSelectedItem();
			TechnologyLevel destination = (TechnologyLevel)destinationBox.getSelectedItem();
			 int amount = (int)troopAmountBox.getSelectedItem();
		  	 temp = new UpgradeTroop(territory, player, source, destination, amount);
		  	 if (temp.checkOrder()){
			  	    textPanel.addHistory(temp);
			  	    temp.executeOrder();
			  	  textPanel.updatePlayerPanel();
			  	territoryPanel.update();
			     }
		  	 upgradeOrderFrame.dispose();
		}
		 }
	 
	}
}
